var latestAnnouncements = "";
var announcementIter = 25;

var simpleArrayStore = [];
var announcementData = [];
var store = new Ext.data.SimpleStore({
    fields: [
        {
            name: 'id',
            type: 'int'
        },
        {
            name: 'name'
        },
        {
            name: 'position'
        },
        {
            name: 'ambition'
        }
    ]
});

function loadAnnouncements(home) {
    Ext.getDom("announcements-div").innerHTML = "";
    latestAnnouncements = "";
    announcementIter = 25;
    Ext.Ajax.request({
        url: "/addama-rest/dudley-sample/path/announcement/annotations",
        method: "GET",
        success: function(response) {
            var jsonObj = Ext.util.JSON.decode(response.responseText);
            var annCount = jsonObj.ann_entry;
            populateAnnouncementDiv(annCount, home);
        }
    });
}
;

function seeAllAnnouncements() {
    alert("ToDo: Function has not been built");
}
;


function populateAnnouncementDiv(annCount, home) {

    var annStore = null;
    if (annCount > 0 && announcementIter >= 1) {
        //Ext.getDom("announcements-all-div").innerHTML = "<A HREF='javascript:seeAllAnnouncements()' >View All</A>";
        Ext.Ajax.request({
            url: "/addama-rest/dudley-sample/path/announcement/" + "ann" + annCount + "/annotations",
            method: "GET",
            success: function(response) {
                var jsonObj = Ext.util.JSON.decode(response.responseText);
                var annText = jsonObj.text;
                var annDate = jsonObj.annDate;
                var ja = [];
                ja[0] = announcementIter,ja[1] = jsonObj.annDate,ja[2] = jsonObj.text;
                announcementData[25 - (announcementIter + 1)] = ja;
                latestAnnouncements = latestAnnouncements + annDate + " " + annText + "<br>";
                if (home) {
                    Ext.getDom("announcements-div").innerHTML = latestAnnouncements;
                }
                announcementIter = announcementIter - 1;
                populateAnnouncementDiv(annCount - 1, home);
                if (announcementIter < 1) {
                    annStore = new Ext.ux.data.PagingArrayStore({
                        fields: [
                            {
                                name: 'id'
                            },
                            {
                                name: 'date'
                            },
                            {
                                name: 'text'
                            }

                        ],
                        data: announcementData,
                        sortInfo: {field:'id', direction:'DESC'}
                    });
                    annStore.load({params:{start:0, limit:25}});
                    var annpanel = new Ext.grid.GridPanel({
                        id: "announcementSearchResultsId",
                        store: annStore,
                        columns: [
                            {
                                id:'id',
                                header: 'id',
                                width: 30,
                                sortable: true,
                                dataIndex: 'id'
                            },
                            {
                                id:'date',
                                header: 'date',
                                width: 70,
                                sortable: true,
                                dataIndex: 'date'
                            },
                            {
                                id:'text',
                                header: 'text',
                                width: 450,
                                sortable: true,
                                dataIndex: 'text'
                            }
                        ],
                        stripeRows: true,
                        autoHeight: true,
                        width: 675,
                        title: 'Latest Sequence Announcements',
                        bbar: new Ext.PagingToolbar({
                            pageSize: 25,
                            store: annStore,
                            displayInfo: true,
                            displayMsg: 'Displaying strains {0} - {1} of {2}',
                            emptyMsg: "No strains to display"
                        })
                    });

                    if (!home) {
                        new Ext.Window({
                            title: 'Omics Sequencing Status Announcements',
                            width: 675,
                            height: 600,
                            autoScroll: true,
                            modal: true,
                            bodyStyle: 'padding:2px;',
                            items: annpanel,
                            layout: 'fit'
                        }).show();
                    }
                }
            },
            failure: function () {
                Ext.getDom("announcements-div").innerHTML = "Failed getting LIMS Announcements -> " + new Date();
            }
        });
    }
}
;
